package com.nikanbujian.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nikanbujian.reggie.common.CustomException;
import com.nikanbujian.reggie.entity.Category;
import com.nikanbujian.reggie.entity.Dish;
import com.nikanbujian.reggie.entity.Setmeal;
import com.nikanbujian.reggie.mapper.CategoryMapper;
import com.nikanbujian.reggie.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nikanbujian.reggie.service.IDishService;
import com.nikanbujian.reggie.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-19
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    IDishService iDishService;
    @Autowired
    ISetmealService iSetmealService;
    @Override
    public void removeById(Long id) {

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count = iDishService.count(dishLambdaQueryWrapper);
        if (count>0){
            throw new CustomException("有关联菜品，无法删除");
        }
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count1 = iSetmealService.count(lambdaQueryWrapper);
        if (count1>0){
            throw new CustomException("有关联套餐，无法删除");
        }

        super.removeById(id);
    }
}
